package com.wisdytech.linkdcs.pda.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.linkdcs.common.BaseUtils;
import com.wisdytech.linkdcs.common.PojoConvertUtil;
import com.wisdytech.linkdcs.equipLifeCycle.check.dto.CheckSearchDto;
import com.wisdytech.linkdcs.equipLifeCycle.check.model.CheckModelItem;
import com.wisdytech.linkdcs.equipLifeCycle.check.vo.CheckVo;
import com.wisdytech.linkdcs.pda.dto.CheckItemDto;
import com.wisdytech.linkdcs.pda.dto.CheckItemSaveDto;
import com.wisdytech.linkdcs.pda.service.ICheckService;

/**
 * 点检相关接口
 * @author dlb
 *
 */
@RestController
@RequestMapping("/check")
public class CheckAction {
	
	private Logger logger =LoggerFactory.getLogger(getClass());
	
	@Resource
	private ICheckService iCheckService;
	
	@RequestMapping("/waitCheckList")
	public StandardResult waitCheckList(String queryData, Filter filter) {
		
		/*if(StringUtils.isBlank(queryData)) {
			logger.info("查询待点检项目设备编码为空");
			return StandardResult.getFailResult("设备编码为空");
		}*/
		
		
		CheckSearchDto dto=new CheckSearchDto();
		dto.setEquipCode(queryData);
		
		PageInfo<CheckVo> pageInfos=
				iCheckService.listEquipChecks(filter, dto);
		
		
		Map<String, Object> result =
				BaseUtils.getRetResult("listData", pageInfos.getList());
		result.put("totalPage", pageInfos.getPages());
		
		return StandardResult.getSuccessResult(result);
	}
	
	@RequestMapping("/checkDetail")
	public StandardResult checkDetail(String checkId) {
		
		if(StringUtils.isBlank(checkId)) {
			logger.info("点检详情查询 待点检id为空");
			return StandardResult.getFailResult("待点检id为空");
		}
		Filter filter =new Filter();
		filter.setNum(1);
		filter.setSize(Integer.MAX_VALUE);
		
		PageInfo<CheckModelItem>  pageInfos =
				iCheckService.listCheckModelItems(filter, checkId);
		
		List<CheckItemDto>  itemDtos=
				PojoConvertUtil.convertPojos(pageInfos.getList(), CheckItemDto.class);
		
		Map<String, Object> result =
				BaseUtils.getRetResult("checkItem", itemDtos);
		
		
		return StandardResult.getSuccessResult(result);
	}
	
	@RequestMapping("/saveCheck")
	public StandardResult saveCheck(String checker,String equipCode,String username) {
		
		if(StringUtils.isBlank(checker)) {
			return StandardResult.getFailResult("点检项为空");
		}
		
		CheckItemSaveDto checkItemDto =
				JSON.parseObject(checker, CheckItemSaveDto.class);
		
		boolean result =
				iCheckService.addCheckRecord(checkItemDto.getCheckItem(), equipCode, checkItemDto.getTotalTime(), username);
		
		return result?StandardResult.getSuccessResult():
				StandardResult.getFailResult("保存失败");
	}
	
	
}
